﻿//DUCNV ham load giao dien number phan trang
//page - trang muon load
//curpage - trang hien tai
function pages(page,curpage,count) {
    var start = parseInt(page) - 2;
    var end = parseInt(page) + 2;
    if (parseInt(count) > 1) {
        if (parseInt(page) <= 3 || parseInt(count) < 7) {
            start = 2;
            end = 6;
        }
        else if (parseInt(page) >= parseInt(count) - 2) {
            start = start - 3 + parseInt(count) - parseInt(page);
            end = end - 3 + parseInt(count) - parseInt(page);
        }
        if (parseInt(count) < end) {
            end = parseInt(count);
        }
        $("#load_page").html('');
        if (parseInt(curpage) > 1) {
            $("#load_page").append('<div class="page" onclick="loadpages(' + (parseInt(curpage) - 1) + ')">Trước</div>');
        }
        if (parseInt(curpage) == 1) {
            $("#load_page").append('<div class="page_curen" onclick="loadpages(1)">1</div>');
        } else {
            $("#load_page").append('<div class="page" onclick="loadpages(1)">1</div>');
        }
        if (parseInt(page) > 4 && parseInt(count) > 7) {
            $("#load_page").append('<div class="page" onclick="pages(' + (parseInt(start) - 3) + ',' + curpage + ',' + count + ')">←</div>');
        }
        for (var i = start; i <= end; i++) {
            if (parseInt(curpage) == i) {
                $("#load_page").append('<div class="page_curen" onclick="loadpages(' + i + ')">' + i + '</div>');
            } else {
                $("#load_page").append('<div class="page" onclick="loadpages(' + i + ')">' + i + '</div>');
            }
        }
        if (parseInt(count) - parseInt(page) >= 4 && parseInt(count) > 7) {
            $("#load_page").append('<div class="page" onclick="pages(' + (parseInt(end) + 3) + ',' + curpage + ',' + count + ')">→</div>');
        }
        if (parseInt(curpage) == parseInt(count) && parseInt(count) != end) {
            $("#load_page").append('<div class="page_curen" onclick="loadpages(' + count + ')">' + count + '</div>');
        } else if (parseInt(count) != end) {
            $("#load_page").append('<div class="page" onclick="loadpages(' + count + ')">' + count + '</div>');
        }
        if (parseInt(curpage) < parseInt(count)) {
            $("#load_page").append('<div class="page" onclick="loadpages(' + (parseInt(curpage) + 1) + ')">Sau</div>');
        }
    }
}